package com.zhhs.galaxyott.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.zhhs.galaxyott.R;

public class OrderTipDialog extends AlertDialog implements android.view.View.OnClickListener {

    private Context mContext;
    private View view;
    private TextView tip_title, tip_msg, tip_msg_2;
    private Button cancelButton, confirmButton;

    private DialogCallback callback;
    private String msgString;
    private boolean OnlyTip = false;

    public OrderTipDialog(Context context, String msg, DialogCallback callback) {
        super(context);
        this.mContext = context;
        this.callback = callback;
        this.msgString = msg;
        OnlyTip = false;

    }

    public OrderTipDialog(Context context, String msg, DialogCallback callback, boolean OnlyTip) {
        super(context);
        this.mContext = context;
        this.callback = callback;
        this.msgString = msg;
        this.OnlyTip = OnlyTip;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i("dialog", "this is onCreate-->1");
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new ColorDrawable());
        setContentView(R.layout.login_out_dialog);

        initView();
    }

    private void initView() {
        setSize();
        cancelButton = (Button) findViewById(R.id.cancel);
        confirmButton = (Button) findViewById(R.id.confirm);
        tip_title = (TextView) findViewById(R.id.tip_title);
        tip_msg = (TextView) findViewById(R.id.tip_msg);
        tip_msg_2 = (TextView) findViewById(R.id.tip_msg_2);

        tip_msg_2.setText("购买影片的费用将从您有线账户余额中扣除，请保持您的账户余额充足。");

        if (OnlyTip) {
            tip_msg_2.setVisibility(View.GONE);
        } else {
            tip_msg_2.setVisibility(View.VISIBLE);
        }

        SpannableStringBuilder builder = new SpannableStringBuilder(StringUtils.toDBC(msgString));
        ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
        ForegroundColorSpan greenSpan = new ForegroundColorSpan(Color.GREEN);

        int start = msgString.indexOf("为") + 1;
        int end = msgString.indexOf("元");

        builder.setSpan(redSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        tip_msg.setText(builder);
        // tip_msg.setText(StringUtils.toDBC(msgString));
        cancelButton.setOnClickListener(this);
        confirmButton.setOnClickListener(this);

        cancelButton.requestFocus();

        if (OnlyTip) {
            cancelButton.setVisibility(View.GONE);
            findViewById(R.id.blank_1).setVisibility(View.GONE);
            confirmButton.requestFocus();
        }
    }

    private void setSize() {
        DisplayMetrics dm = new DisplayMetrics();
        // 获取屏幕对象
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels; // 屏幕宽（像素，如：480px）
        int height = dm.heightPixels; // 屏幕高（像素，如：800px）
        Window dialogWindow = this.getWindow();
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.height = (int) (height * 0.45);
        p.width = (int) (width * 0.50);
        dialogWindow.setAttributes(p);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
        case R.id.cancel:
            callback.Cancel();
            dismiss();
            break;
        case R.id.confirm:
            // loginOut();
            callback.Confirm();

            break;

        default:
            break;
        }
    }

    public interface DialogCallback {

        void Confirm();

        void Cancel();

    }

}
